package cn.com.xy.sms.sdk.ui.popu.util;

import java.util.Date;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.xy.bizport.data.MyDate;
import com.xy.bizport.data.MyThrowable;

import cn.com.xy.sms.sdk.action.XyIpcAidlAction;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.XyJSONObject;

public class IpcAidlActionImp extends XyIpcAidlAction {
    private final static String TAG = "IpcAidlActionImp";

    @Override
    public boolean supportAidlAction() {
        return true;
    }

    @Override
    public Map<String, Object> parseMsgToBaseMap(String sender, String content, Map<String, String> map) {

        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable().parseMsgToBaseMap(sender, content,
                    String.valueOf(XyJSONObject.getFromMap(map)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseMsgToBaseMap", t);
        }
        return retMap;
    }

    @Override
    public Map<String, Object> parseVerifyCodeToMap(String sender, String content, Map<String, String> extend) {

        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable().parseVerifyCodeToMap(sender, content,
                    String.valueOf(XyJSONObject.getFromMap(extend)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseVerifyCodeToMap", t);
        }
        return retMap;
    }

    @Override
    public Map<String, Object> handerBubbleValueMap(Map<String, Object> valueMap) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerBubbleValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerBubbleValueMap", t);
        }

        return retMap;
    }

    @Override
    public String catchUrls(String msg, String unionBy) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().catchUrls(msg, unionBy);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.catchUrls", t);
        }

        return null;
    }

    @Override
    public int parseSensitive(String message) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().parseSensitive(message);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseSensitive", t);
        }

        return 0;
    }

    @Override
    public String parseRecogniseValue(String phoneNum, String msg, long msgTime, Map<String, Object> valueMap) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().parseRecogniseValue(phoneNum, msg, msgTime,
                    String.valueOf(XyJSONObject.getFromMap(valueMap)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseRecogniseValue", t);
        }

        return null;
    }

    @Override
    public String getRecogniseActionConfig(JSONObject obj, Map<String, String> extend) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().getRecogniseActionConfig(String.valueOf(obj),
                    String.valueOf(XyJSONObject.getFromMap(extend)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getRecogniseActionConfig", t);
        }

        return null;
    }

    @Override
    public void handleParseMsg(String msgId, String phoneNumber, String smsCenterNum, String smsContent, long smsTime,
            Map<String, String> extend, Map<String, Object> result) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().handleParseMsg(msgId, phoneNumber, smsCenterNum, smsContent,
                    smsTime, String.valueOf(XyJSONObject.getFromMap(extend)),
                    String.valueOf(XyJSONObject.getFromMap(result)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handleParseMsg", t);
        }
    }

    @Override
    public int getSmsTypeByMap(Map<String, Object> resultMap, int juagetype) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable()
                    .getSmsTypeByMap(String.valueOf(XyJSONObject.getFromMap(resultMap)), juagetype);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getSmsTypeByMap", t);
        }

        return -1;
    }

    @Override
    public JSONObject getSmsType(Map<String, Object> resultMap) {
        JSONObject retObj = null;
        try {
            String response = XyIpcUtil.getInstance().getRemoteCallable()
                    .getSmsType(String.valueOf(XyJSONObject.getFromMap(resultMap)));
            retObj = new JSONObject(response);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getSmsType", t);
        }

        return retObj;
    }

    @Override
    public Map<String, Object> handerNotificationValueMap(Map<String, Object> valueMap) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerNotificationValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerNotificationValueMap", t);
        }

        return retMap;
    }

    @Override
    public Map<String, Object> handerDateValueMap(Map<String, Object> valueMap) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerDateValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerDateValueMap", t);
        }

        return retMap;
    }

    @Override
    public Map<String, Object> handerRemindValueMap(Map<String, Object> valueMap) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerRemindValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerRemindValueMap", t);
        }

        return retMap;
    }

    @Override
    public Map<String, Object> handerPayValueMap(Map<String, Object> valueMap) {

        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerPayValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerPayValueMap", t);
        }

        return retMap;
    }

    @Override
    public Map<String, Object> handerValueMap(Map<String, Object> valueMap) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerValueMap", t);
        }

        return retMap;
    }

    @Override
    public Map<String, Object> handerValueMap(Map<String, Object> valueMap, String titleNo) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerValueMapWithTitle(String.valueOf(XyJSONObject.getFromMap(valueMap)), titleNo);
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerValueMap", t);
        }

        return retMap;
    }

    @Override
    public boolean isOperatorsPhoneType(String phone) {
        boolean type = false;
        try {
            type = XyIpcUtil.getInstance().getRemoteCallable().isOperatorsPhoneType(phone);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.isOperatorsPhoneType", t);
        }

        return type;
    }

    @Override
    public String getBubbleViewVersion(Map<String, Object> extend) {
        String opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .getBubbleViewVersion(String.valueOf(XyJSONObject.getFromMap(extend)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getBubbleViewVersion", t);
        }
        return opRet;
    }

    @Override
    public boolean isAppChannel(String appKey) {
        boolean opRet = false;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().isAppChannel(appKey);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.isAppChannel", t);
        }

        return opRet;
    }

    @Override
    public boolean isEnterpriseSms(String phoneNumber, String smsContent, Map<String, String> extend) {
        boolean opRet = false;
        
        try {
            String tempStr = null;
            if (extend!=null) {
                tempStr = XyJSONObject.getFromMap(extend).toString();
            }
            opRet = XyIpcUtil.getInstance().getRemoteCallable().isEnterpriseSms(phoneNumber, smsContent, tempStr);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.isEnterpriseSms", t);
        }
        return opRet;
    }

    @Override
    public int getActionCode(String action) {
        int opRet = -1;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().getActionCode(action);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getActionCode", t);
        }

        return opRet;
    }

    @Override
    public String[] parseMsgToNewContacts(String phonenum, String msg, String oldContact, String[] contactNames) {
        String[] opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().parseMsgToNewContacts(phonenum, msg, oldContact,
                    contactNames);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseMsgToNewContacts", t);
        }

        return opRet;
    }

    @Override
    public int isServiceChoose(String channel, String type) {
        int opRet = -1;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().isServiceChoose(channel, type);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.isServiceChoose", t);
        }

        return opRet;
    }

    @Override
    public Map<String, Object> handerValueMapByType(int type, Map<String, Object> valueMap) {
        Map<String, Object> retMap = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable().handerValueMapByType(type,
                    String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retMap = new XyJSONObject(opRet).getMap();
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerValueMapByType", t);
        }

        return retMap;
    }

    @Override
    public String handerWatchValueMap(Map<String, Object> valueMap) {
        String opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerWatchValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerWatchValueMap", t);
        }

        return opRet;
    }

    @Override
    public JSONObject handerContactValueMap(Map<String, Object> valueMap) {
        JSONObject retObj = null;
        try {
            String opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerContactValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
            if (opRet != null) {
                retObj = new JSONObject(opRet);
            }
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerContactValueMap", t);
        }

        return retObj;
    }

    @Override
    public String handerBoxValueMap(Map<String, Object> valueMap) {
        String opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable()
                    .handerBoxValueMap(String.valueOf(XyJSONObject.getFromMap(valueMap)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.handerBoxValueMap", t);
        }

        return opRet;
    }

    @Override
    public String getCmd(String phoneNum, String msg) {
        String opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().getCmd(phoneNum, msg);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getCmd", t);
        }

        return opRet;
    }

    @Override
    public String parseMsgForCardArray(JSONObject jsobj, Map<String, String> extend) {
        String opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().parseMsgForCardArray(String.valueOf(jsobj),
                    String.valueOf(XyJSONObject.getFromMap(extend)));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseMsgForCardArray", t);
        }

        return opRet;
    }

    @Override
    public String multiReplace(String msg) {
        String opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().multiReplace(msg);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.multiReplace", t);
        }

        return opRet;
    }

    @Override
    public String[] parseShard(String msg, String msgIdx, String msgMd) {
        String[] opRet = null;
        try {
            opRet = XyIpcUtil.getInstance().getRemoteCallable().parseShard(msg, msgIdx, msgMd);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseShard", t);
        }

        return opRet;
    }

    @Override
    public String parseEmail(String fromUser, String content, Map<String, String> extend) {
        // TODO
        return super.parseEmail(fromUser, content, extend);
    }

    @Override
    public String getSceneVersion() {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().getSceneVersion();
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getSceneVersion", t);
        }

        return null;
    }

    @Override
    public boolean getOnOffByType(final int type) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().isOnOff(type, null);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getOnOffByType", t);
        }
        return false;
    }

    @Override
    public JSONArray getConfigByType(final int type, final String uiVersion, final Integer menuId) {
        try {
            String data = XyIpcUtil.getInstance().getRemoteCallable().getConfigData(type, null, null, uiVersion,
                    menuId);
            return new JSONArray(data);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getConfigByType", t);
        }

        return null;
    }

    @Override
    public String getOnLineConfigureData(int type) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().getConfigString(type, null);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getOnLineConfigureData", t);
        }

        return null;
    }

    @Override
    public long getUpdateCycle(int type, long defaultValue) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().getUpdateCycle(type, defaultValue);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getUpdateCycle", t);
        }

        return 0L;
    }

    private String[] convertParamsToStringArray(Object[] params) {
        Object[] src = DexUtil.handlerParamsToJSONObjectIfNeed(params);
        if (src == null || src.length == 0) {
            return null;
        }

        String[] result = new String[params.length];
        for (int i = 0; i < src.length; i++) {
            if (src[i] == null) {
                result[i] = "";
            } else {
                result[i] = src[i].toString();
            }
        }

        return result;
    }

    @Override
    public void saveLogIn(final String uuid, final String className, final String methodName, final Object... args) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().saveLogIn(uuid, className, methodName,
                    convertParamsToStringArray(args));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.saveLogIn", t);
        }
    }

    @Override
    public void saveLogOut(final String uuid, final String className, final String methodName, final Object... args) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().saveLogOut(uuid, className, methodName,
                    convertParamsToStringArray(args));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.saveLogOut", t);
        }
    }

    @Override
    public void saveLogException(final String className, final String methodName, Throwable tt) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().saveLogException(className, methodName, new MyThrowable(tt));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.saveLogException", t);
        }
    }

    @Override
    public void insertLog(final String table, final String sql, final JSONObject value) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().insertLog(table, sql, String.valueOf(value));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.insertLog", t);
        }
    }

    @Override
    public void deleteLog(final String table, final String sql, final JSONObject whereArgs) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().deleteLog(table, sql, String.valueOf(whereArgs));
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.deleteLog", t);
        }

    }

    @Override
    public JSONArray queryLog(final String table, final String sql, final String[] columns, final JSONObject whereArgs,
            final String orderBy, final String limit) {
        try {
            String data = XyIpcUtil.getInstance().getRemoteCallable().queryLog(table, sql, columns,
                    String.valueOf(whereArgs), orderBy, limit);
            return new JSONArray(data);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.queryLog", t);
        }

        return null;
    }

    @Override
    public String queryLogByType(int type, final String channel, final String appVersion) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().queryLogByType(type, channel, appVersion);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.queryLogByType", t);
        }

        return null;
    }

    @Override
    public void postLogCallback(int type, final String data) {
        try {
            XyIpcUtil.getInstance().getRemoteCallable().postLogCallback(type, data);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.postLogCallback", t);
        }
    }

    @Override
    public String getCorp(final String msg) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().getCorp(msg);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getCorp", t);
        }

        return null;
    }

    @Override
    public String[] getCorpAndEc(final String msg) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().getCorpAndEc(msg);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.getCorpAndEc", t);
        }

        return null;
    }

    // DateTimeNormalizer
    @Override
    public Date convertDate(final String dateString) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().convertDate(dateString, new MyDate(), true, false);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.convertDate", t);
        }

        return null;
    }

    public Date convertDate(final String dateString, Date timeBase) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().convertDate(dateString, new MyDate(timeBase), true,
                    false);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.convertDate", t);
        }

        return null;
    }

    @Override
    public long convertDateLong(String dateString, Date timeBase, boolean autoFill, boolean ignoreIfNoDate,
            boolean maxThanBaseTime) {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().convertDateLong(dateString, new MyDate(timeBase),
                    autoFill, ignoreIfNoDate, maxThanBaseTime);
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.convertDateLong", t);
        }

        return 0L;
    }

    @Override
    public boolean parseUtilMainJarIsReady() {
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().parseUtilMainJarIsReady();
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.parseUtilMainJarIsReady", t);
        }
        return false;
    }

    @Override
    public boolean isInitData() {
        // TODO Auto-generated method stub
        try {
            return XyIpcUtil.getInstance().getRemoteCallable().isInitData();
        } catch (Throwable t) {
            LogManager.e(TAG, "Ipc.isInitData", t);
        }
        return false;
    }

}
